package org.gogenie.android.ui;

import org.gogenie.android.model.Post;

import uy.com.silveira.noname.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

/**
 * Show the user back his picture and get description and location.
 * @author jjso
 */
public class EnterTextActivity extends Activity {

  public static String EXTRA_POST = "Post";

  private Post post;

  private OnClickListener saveAndContinueClickListener = new OnClickListener() {
    public void onClick(View v) {
      Log.d("NoName", "Save and continue onClick");
      openCategorySelection();
    }
  };

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.enter_text);
    ((Button) findViewById(R.id.save_and_continue_btn)).setOnClickListener(
        saveAndContinueClickListener);

    ((ImageView) findViewById(R.id.image_viewer)).setOnClickListener(
        saveAndContinueClickListener);

    post = (Post) getIntent().getSerializableExtra(EXTRA_POST);
    if (post.getImagePath() != null) {
      showImage(post.getImagePath());
    }
  }

  private void showImage(String imagePath) {
    Drawable image = BitmapDrawable.createFromPath(imagePath);
    ImageView imageViewer = (ImageView)findViewById(R.id.image_viewer);
    imageViewer.setImageDrawable(image);
  }

  private String getDescriptionText() {
    return ((EditText) findViewById(R.id.description_input)).getText().toString();
  }

  private void openCategorySelection() {
    Log.d("NoName", "About to open category selection");
    post.setDescription(getDescriptionText());

    Intent openCategorySelectionActivity = new Intent(
        this, CategorySelectionActivity.class);
    openCategorySelectionActivity.putExtra(CategorySelectionActivity.EXTRA_POST, post);
    startActivity(openCategorySelectionActivity);
  }
}
